package shapes.shapes2D.distribution 
{
	import flash.geom.Point;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Disc
	{
		
		public var center:Point;
		public var radiusX:Number;
		public var radiusY:Number;
		public var innerRadiusX:Number;
		public var innerRadiusY:Number;
		private var points:Vector.<Point>;
		
		public function Disc( center:Point, radiusX:Number, radiusY:Number, innerRadiusX:Number = 0, innerRadiusY:Number = 0 ) 
		{
			this.center = center;
			this.radiusX = radiusX;
			this.radiusY = radiusY;
			this.innerRadiusX = innerRadiusX;
			this.innerRadiusY = innerRadiusY;
		}
		
		
		public function distribute( count:uint, shuffle:Boolean = false, jitter:Number = 0 ):Vector.<Point>
		{
			var p:Point;
			points = new Vector.<Point>();
			
			var length:int = count;
			
			if ( jitter == 0 ) length /= 2;
			
			var i:int, angle:Number, depart:Number, angleStep:Number, offsetX:Number, offsetY:Number, dx:Number, dy:Number;
			
			offsetX = radiusX - innerRadiusX;
			offsetY = radiusY - innerRadiusY;
			
			angleStep = ( Math.PI * 2 ) / length;
			
			for ( i = 0; i < length; i++ )
			{
				
				angle = i * angleStep;
				
				if ( jitter != 0 )
				{
					p = new Point( 0, 0 );
					p.x = center.x + Math.cos( angle ) * ( innerRadiusX + Math.random() * offsetX );
					p.y = center.y + Math.sin( angle ) * ( innerRadiusY + Math.random() * offsetY );
					points.push( p );
				}
				else
				{
					p = new Point();
					p.x = center.x + Math.cos( angle ) * radiusX;
					p.y = center.y + Math.sin( angle ) * radiusY;
					points.push( p );
					
					p = new Point();
					p.x = center.x + Math.cos( angle ) * innerRadiusX;
					p.y = center.y + Math.sin( angle ) * innerRadiusY;
					points.push( p );
				}
				
				
			}
			
			if ( shuffle ) 	points.sort( this.shuffle );
			if ( jitter != 0 )	jitterPoints( jitter );
			
			return points;
			
		}
		
		private function shuffle( a:Number, b:Number ):int
		{
			return ( Math.random() > .5 ) ? 1 : -1;
		}

		private function jitterPoints( radius:Number ):void
		{
			var rad:Number;
			var angle:Number;
			for each( var p:Point in points )
			{
				
				rad = radius * Math.random();
				angle = ( 2 * Math.PI ) * Math.random();
				p.x += rad * Math.cos(angle);
				p.y += rad * Math.sin(angle);
				
			}
		}
	}

}